from alipay import AliPay
from django.conf import settings
from django.shortcuts import render
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView

#支付宝支付
from apps.orders.models import OrderInfo
from apps.pays.models import Payment


class AlipayView(APIView):
    def get(self,request,order_id):

        # 0,获取order对象
        try:
            order = OrderInfo.objects.get(order_id=order_id)
        except Exception as e:
            return Response(status=status.HTTP_400_BAD_REQUEST)

        # 1,准备公钥私钥
        # app_private_key_string=settings.APP_PRIVATE_KEY_PATH
        # alipay_public_key_string =settings.ALIPAY_PUBLIC_KEY_PATH
        with open(settings.APP_PRIVATE_KEY_PATH,'r') as f:
            app_private_key_string=f.read()

        with open(settings.ALIPAY_PUBLIC_KEY_PATH,'r') as f:
            alipay_public_key_string=f.read()

        #创建支付宝对象
        alipay = AliPay(
            appid=settings.ALIPAY_APPID,
            app_notify_url=None,  # 默认回调url
            app_private_key_string=app_private_key_string,
            # 支付宝的公钥，验证支付宝回传消息使用，不是你自己的公钥,
            alipay_public_key_string=alipay_public_key_string,
            sign_type="RSA2",  # RSA 或者 RSA2
            debug = False  # 默认False
        )


        #链接支付宝，创建支付宝订单对象
        order_string = alipay.api_alipay_trade_page_pay(
            out_trade_no=order_id,
            total_amount=str(order.total_amount),
            subject="澳门首家线上赌场",
            return_url="http://www.meiduo.site:8080/pay_success.html",
        )

        #支付宝支付网址
        alipay_url=settings.ALIPAY_URL+"?"+order_string

        #返回给前段
        return Response({"alipay_url": alipay_url})

class AlipayOrderStatusView(APIView):
    def put(self,request):
        #获取参数
        data = request.query_params.dict()
        # sign 不能参与签名验证
        signature = data.pop("sign")

        # 1,准备公钥私钥
        with open(settings.APP_PRIVATE_KEY_PATH, 'r') as f:
            app_private_key_string = f.read()

        with open(settings.ALIPAY_PUBLIC_KEY_PATH, 'r') as f:
            alipay_public_key_string = f.read()

        # 创建支付宝支付对象
        alipay = AliPay(
            appid=settings.ALIPAY_APPID,
            app_notify_url=None,  # 默认回调url
            app_private_key_string=app_private_key_string,
            # 支付宝的公钥，验证支付宝回传消息使用，不是你自己的公钥,
            alipay_public_key_string=alipay_public_key_string,
            sign_type="RSA2",  # RSA 或者 RSA2
            debug=False  # 默认False
        )

        # 认证
        success = alipay.verify(data, signature)

        #认证失败，则返回400
        if not success :
            return Response(status=status.HTTP_400_BAD_REQUEST)

        #拿到美多订单id
        order_id = data.get("out_trade_no")
        #拿到支付宝的订单id
        trade_id = data.get("trade_no")

        #创建支付宝订单跟美多订单绑定对象
        Payment.objects.create(
            order_id=order_id,
            trade_id=trade_id
        )

        # 3.2 修改订单信息状态
        OrderInfo.objects.filter(order_id=order_id).update(status=OrderInfo.ORDER_STATUS_ENUM["UNSEND"])

        # 返回响应
        return Response({"trade_id":trade_id})